/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package de.thwildau.tm.moses.abnf.formatting;

import static de.thwildau.tm.moses.abnf.util.AbnfKeywords.CLOSING_BRACKET;
import static de.thwildau.tm.moses.abnf.util.AbnfKeywords.CLOSING_SQUARED_BRACKET;
import static de.thwildau.tm.moses.abnf.util.AbnfKeywords.EQUAL;
import static de.thwildau.tm.moses.abnf.util.AbnfKeywords.MINUS;
import static de.thwildau.tm.moses.abnf.util.AbnfKeywords.OPENING_BRACKET;
import static de.thwildau.tm.moses.abnf.util.AbnfKeywords.OPENING_SQUARED_BRACKET;
import static de.thwildau.tm.moses.abnf.util.AbnfKeywords.PLUS;
import static de.thwildau.tm.moses.abnf.util.AbnfKeywords.SLASH;
import static de.thwildau.tm.moses.abnf.util.AbnfKeywords.STAR;

import java.util.List;

import org.eclipse.xtext.Keyword;
import org.eclipse.xtext.formatting.impl.AbstractDeclarativeFormatter;
import org.eclipse.xtext.formatting.impl.FormattingConfig;
import org.eclipse.xtext.util.Pair;

import de.thwildau.tm.moses.abnf.services.AbnfGrammarAccess;

/**
 * This class contains custom formatting description.
 * 
 * @author Thomas Kistel
 * @version $Revision: 762 $
 * @see http://www.eclipse.org/Xtext/documentation/latest/xtext.html#formatting
 */
public class AbnfFormatter extends AbstractDeclarativeFormatter {
	private static final String SPACE = " ";

	@Override
	protected void configureFormatting(FormattingConfig c) {
		AbnfGrammarAccess g = (AbnfGrammarAccess) getGrammarAccess();
		c.setAutoLinewrap(80);
		// It's usually a good idea to activate the following three statements.
		// They will add and preserve newlines around comments
		c.setLinewrap(0, 1, 2).before(g.getSL_COMMENTRule());
		c.setLinewrap(0, 1, 1).after(g.getSL_COMMENTRule());
		// c.setLinewrap(0, 1, 2).before(g.getML_COMMENTRule());
		// c.setLinewrap(0, 1, 1).after(g.getML_COMMENTRule());
		// Remove Linebreaks and add spaces around '='
		for (Keyword k : g.findKeywords(EQUAL.toString())) {
			c.setNoLinewrap().before(k);
			c.setSpace(SPACE).around(k);
		}

		// LineBreaks and Increment after '='
		for (Keyword k : g.findKeywords(EQUAL.toString())) {
			// c.setLinewrap(1).after(k);
			// c.setSpace(TAB).after(k);
			c.setIndentationIncrement().after(k);
			c.setIndentationIncrement().after(k);
		}
		// LineBreaks and Decrement after Elements
		c.setLinewrap(1).after(g.getAlternationRule());
		c.setIndentationDecrement().after(g.getAlternationRule());
		c.setIndentationDecrement().after(g.getAlternationRule());

		// Remove spaces around '-'
		for (Keyword k : g.findKeywords(MINUS.toString())) {
			c.setNoSpace().around(k);
		}
		// Remove spaces around '+'
		for (Keyword k : g.findKeywords(PLUS.toString())) {
			c.setNoSpace().around(k);
		}
		// Remove Linebreaks around '*'
		for (Keyword k : g.findKeywords(STAR.toString())) {
			c.setNoLinewrap().around(k);
		}
		// c.setNoLinewrap().before(g.getRepeatRule());
		// Remove spaces for Brackets
		List<Pair<Keyword, Keyword>> brackets = g.findKeywordPairs(
				OPENING_BRACKET.toString(), CLOSING_BRACKET.toString());
		for (Pair<Keyword, Keyword> pair : brackets) {
			c.setNoLinewrap().before(pair.getFirst());
			c.setNoSpace().after(pair.getFirst());
			c.setNoSpace().before(pair.getSecond());
		}
		// Remove spaces for Squared Brackets
		List<Pair<Keyword, Keyword>> sBrackets = g.findKeywordPairs(
				OPENING_SQUARED_BRACKET.toString(),
				CLOSING_SQUARED_BRACKET.toString());
		for (Pair<Keyword, Keyword> pair : sBrackets) {
			c.setNoLinewrap().before(pair.getFirst());
			c.setNoSpace().after(pair.getFirst());
			c.setNoSpace().before(pair.getSecond());
		}

		// add LineBreaks after RuleRef

		// remove Linebreaks before Repetition
//		 c.setNoLinewrap().before(g.getRepetitionRule()); // ???
		//remove Linebreaks before RuleRefs
		c.setNoLinewrap().before(g.getRuleRefRule());
		// // add a Linebreak after Repetition
		c.setLinewrap().after(g.getRuleRefRule());
		c.setNoLinewrap().between(g.getRuleRefRule(), g.getSTRINGRule());

		// remove LineBreaks after '/'
		for (Keyword k : g.findKeywords(SLASH.toString())) {
			c.setNoLinewrap().before(k);
		}
		// LineBreaks after 2 Element

		// Increments after '(' and Decrements after ')'
		for (Pair<Keyword, Keyword> pair : brackets) {
			c.setIndentationIncrement().after(pair.getFirst());
			c.setIndentationIncrement().after(pair.getFirst());
			// Decrements after ')'
			c.setIndentationDecrement().after(pair.getSecond());
			c.setIndentationDecrement().after(pair.getSecond());
		}
		
		c.setNoLinewrap().before(g.getRepeatRule());

	}
}
